import request from '@/utils/request'

// ==================== 设备数据统计API ====================

// 获取设备概览统计
export function getOverviewStatistics() {
  return request({
    url: '/energy/statistics/overview',
    method: 'get'
  })
}

// 获取设备能耗统计
export function getEnergyStatistics(params) {
  return request({
    url: '/energy/statistics/energy',
    method: 'get',
    params: params
  })
}

// 获取设备运行状态统计
export function getStatusStatistics(params) {
  return request({
    url: '/energy/statistics/status',
    method: 'get',
    params: params
  })
}

// 获取设备效率分析
export function getEfficiencyAnalysis(deviceCode, params) {
  return request({
    url: '/energy/statistics/efficiency/' + deviceCode,
    method: 'get',
    params: params
  })
}

// 获取设备负载趋势
export function getLoadTrend(deviceCode, params) {
  return request({
    url: '/energy/statistics/load/' + deviceCode,
    method: 'get',
    params: params
  })
}

// 获取设备告警统计
export function getAlarmStatistics(params) {
  return request({
    url: '/energy/statistics/alarm',
    method: 'get',
    params: params
  })
}

// 获取设备对比分析
export function getComparisonAnalysis(params) {
  return request({
    url: '/energy/statistics/comparison',
    method: 'get',
    params: params
  })
}

// 获取设备峰谷电分析
export function getPeakValleyAnalysis(deviceCode, params) {
  return request({
    url: '/energy/statistics/peakvalley/' + deviceCode,
    method: 'get',
    params: params
  })
}

// 获取设备数据质量报告
export function getDataQualityReport(params) {
  return request({
    url: '/energy/statistics/quality',
    method: 'get',
    params: params
  })
}

// 获取设备能耗排行榜
export function getEnergyRanking(params) {
  return request({
    url: '/energy/statistics/ranking',
    method: 'get',
    params: params
  })
}

// 获取设备实时监控数据
export function getRealtimeMonitorData(params) {
  return request({
    url: '/energy/statistics/realtime',
    method: 'get',
    params: params
  })
}

// 导出设备统计报表
export function exportStatisticsReport(params) {
  return request({
    url: '/energy/statistics/export',
    method: 'get',
    params: params,
    responseType: 'blob'
  })
}

// ==================== 便捷查询方法 ====================

// 获取今日设备概览
export function getTodayOverview() {
  const today = new Date()
  const startTime = new Date(today.getFullYear(), today.getMonth(), today.getDate()).toISOString()
  const endTime = today.toISOString()
  
  return getOverviewStatistics({
    startTime: startTime,
    endTime: endTime
  })
}

// 获取设备本周能耗统计
export function getWeeklyEnergyStats(deviceCode) {
  const today = new Date()
  const weekStart = new Date(today.setDate(today.getDate() - today.getDay()))
  const weekEnd = new Date()
  
  return getEnergyStatistics({
    deviceCode: deviceCode,
    startTime: weekStart.toISOString(),
    endTime: weekEnd.toISOString(),
    groupBy: 'day'
  })
}

// 获取设备本月能耗统计
export function getMonthlyEnergyStats(deviceCode) {
  const today = new Date()
  const monthStart = new Date(today.getFullYear(), today.getMonth(), 1)
  const monthEnd = new Date()
  
  return getEnergyStatistics({
    deviceCode: deviceCode,
    startTime: monthStart.toISOString(),
    endTime: monthEnd.toISOString(),
    groupBy: 'day'
  })
}

// 获取设备24小时负载趋势
export function get24HourLoadTrend(deviceCode) {
  const endTime = new Date()
  const startTime = new Date(endTime.getTime() - 24 * 60 * 60 * 1000)
  
  return getLoadTrend(deviceCode, {
    startTime: startTime.toISOString(),
    endTime: endTime.toISOString(),
    interval: '1h'
  })
}

// 获取设备7天负载趋势
export function get7DayLoadTrend(deviceCode) {
  const endTime = new Date()
  const startTime = new Date(endTime.getTime() - 7 * 24 * 60 * 60 * 1000)
  
  return getLoadTrend(deviceCode, {
    startTime: startTime.toISOString(),
    endTime: endTime.toISOString(),
    interval: '4h'
  })
}

// 获取设备本月峰谷电分析
export function getMonthlyPeakValleyAnalysis(deviceCode) {
  const today = new Date()
  const monthStart = new Date(today.getFullYear(), today.getMonth(), 1)
  const monthEnd = new Date()
  
  return getPeakValleyAnalysis(deviceCode, {
    startTime: monthStart.toISOString(),
    endTime: monthEnd.toISOString()
  })
}

// 获取设备本周告警统计
export function getWeeklyAlarmStats(deviceCode) {
  const today = new Date()
  const weekStart = new Date(today.setDate(today.getDate() - today.getDay()))
  const weekEnd = new Date()
  
  return getAlarmStatistics({
    deviceCode: deviceCode,
    startTime: weekStart.toISOString(),
    endTime: weekEnd.toISOString()
  })
}

// 获取设备本月数据质量报告
export function getMonthlyDataQualityReport(deviceCode) {
  const today = new Date()
  const monthStart = new Date(today.getFullYear(), today.getMonth(), 1)
  const monthEnd = new Date()
  
  return getDataQualityReport({
    deviceCode: deviceCode,
    startTime: monthStart.toISOString(),
    endTime: monthEnd.toISOString()
  })
}

// 获取本月能耗排行榜TOP10
export function getMonthlyEnergyRankingTop10(energyType = 'ELECTRIC') {
  const today = new Date()
  const monthStart = new Date(today.getFullYear(), today.getMonth(), 1)
  const monthEnd = new Date()
  
  return getEnergyRanking({
    energyType: energyType,
    startTime: monthStart.toISOString(),
    endTime: monthEnd.toISOString(),
    limit: 10
  })
}
